package com.jtlrm.ckd.util.ContactList;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.base.sdk.util.EditInputFilter;
import com.base.sdk.util.ToastUtil;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.entity.ShiWuEntity;
import com.jtlrm.ckd.entity.ShiWuSelectEntity;
import com.jtlrm.ckd.entity.YinShiDetailAdpterEntity;
import com.jtlrm.ckd.entity.YinShiDetailEntity;

import java.util.List;

public class ShiWuSelectAdapter extends ContactAdapter {

    List<ShiWuSelectEntity> adapterData;
    int currentType;
    Long index = -1l;
    Context context;

    public ShiWuSelectAdapter(Context context, int resource, List<SlideBarEntity> objects
            , List<ShiWuSelectEntity> adapterData, int type, YinShiDetailAdpterEntity yinShiDetailEntity) {
        super(context, resource, objects);
        // 初始化已存在的数据， 将已提交的数据重新设置
        try {
            for (ShiWuEntity entity : adapterData.get(type).getData()) {
                for (YinShiDetailEntity detailEntity : yinShiDetailEntity.getYinShiDetailEntities()) {
                    if ((entity.getId() + "").equals(detailEntity.getFoodId())) {
                        entity.setGram(Double.parseDouble(detailEntity.getGram()));
                    }
                }
            }
        } catch (Exception e) {

        }

        this.adapterData = adapterData;
        this.currentType = type;
        this.context = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        Long foodId = getItem(position).getFoodId();
        ShiWuEntity shiWuEntity = getShiwuEntity(foodId);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.shiwu_select_item, null);
            holder.nameView = convertView.findViewById(R.id.name);
            holder.headerView = convertView.findViewById(R.id.header);
            holder.gramView = convertView.findViewById(R.id.gram);
            holder.jianView = convertView.findViewById(R.id.jian);
            holder.addView = convertView.findViewById(R.id.add);
            holder.gramView.setTag(foodId);
            holder.gramView.addTextChangedListener(new MyTextWather(holder.gramView));
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.gramView.setTag(foodId);
        }


        Double gram = shiWuEntity.getGram();
        if (gram != -1) {
            holder.gramView.setText(gram + "");
        } else {
            holder.gramView.setText("");
        }
        holder.gramView.setTag(foodId);
        holder.gramView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = (Long) v.getTag();
                }
                return false;
            }
        });
        if (index != -1 && index == foodId) {
            // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
            holder.gramView.requestFocus();
        } else {
            holder.gramView.clearFocus();
        }
        InputFilter[] filters = {new EditInputFilter(context)};
        holder.gramView.setFilters(filters);

        holder.nameView.setText(shiWuEntity.getNameStr());
        holder.jianView.setTag(foodId);
        holder.addView.setTag(foodId);
        holder.jianView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Long f = (Long) v.getTag();
                Double gram = getGram(f);
                if (gram > 1) {
                    Double current = --gram;
                    setGram(f, current);
                    notifyDataSetChanged();
                } else {
                    setGram(f, 0.0);
                    notifyDataSetChanged();
                }
            }
        });
        holder.addView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Long f = (Long) v.getTag();
                Double gram = getGram(f);
                if (gram < 2000) {
                    Double current = ++gram;
                    setGram(f, current);
                    notifyDataSetChanged();
                }
            }
        });
        final String header = shiWuEntity.getInitialLetter();
        if (position == 0 || header != null && !header.equals(getItem(position - 1).getInitialLetter())) {
            if (TextUtils.isEmpty(header)) {
                holder.headerView.setVisibility(View.GONE);
            } else {
                holder.headerView.setVisibility(View.VISIBLE);
                holder.headerView.setText(header);
            }
        } else {
            holder.headerView.setVisibility(View.GONE);
        }

        return convertView;
    }

    private static class ViewHolder {
        TextView nameView;
        TextView headerView;
        EditText gramView;
        ImageView jianView;
        ImageView addView;
    }

    class MyTextWather implements TextWatcher {
        EditText editText;

        public MyTextWather(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            Long foodId = (Long) editText.getTag();
            String content = s.toString();
            if (!TextUtils.isEmpty(content) && content.length() < 10) {
                Double value = Double.parseDouble(content);
                if (value > 2000) {
                    setGram(foodId, 2000.0);
                    ToastUtil.TextToast(getContext(), "最大值只能为2000");
                } else if (value < 0) {
                    ToastUtil.TextToast(getContext(), "最小值为0");
                    setGram(foodId, 0.0);
                } else {
                    setGram(foodId, value);
                }
            }
        }
    }


    private Double getGram(Long foodId) {
        try {
            ShiWuEntity shiWuEntity = getShiwuEntity(foodId);
            Double gram = shiWuEntity.getGram();
            return gram;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0.00;
    }

    public void setGram(Long foodId, Double gram) {
        try {
            ShiWuEntity shiWuEntity = getShiwuEntity(foodId);
            shiWuEntity.setGram(gram);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public ShiWuEntity getShiwuEntity(Long foodId) {
        for (ShiWuEntity entity : adapterData.get(currentType).getData()) {
            if ((entity.getId()).equals(foodId)) {
                return entity;
            }
        }
        return null;
    }


    public void setIndex(Long index) {
        this.index = index;
    }
}

